package com.juxing.sdk.util;

/**
 * 字符串工具类
 * @author liu.qingliang
 * @email liuql_mobiler@163.com
 * @date 2014年7月17日
 *
 */
public class StringUtil {
	
	public static final String[] HEX_ARRAYS = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};

	/**
	 *　判断value是否是null或者其length为0
	 * @param value
	 * @return
	 */
	public static boolean isEmpty(String value) {
		return value == null || value.length() == 0;
	}
	/**
	 * 判断value是否不是null且其length>0
	 * @param value
	 * @return
	 */
	public static boolean isNotEmpty(String value) {
		return !isEmpty(value);
	}
	/**
	 * trim操作
	 * @param value
	 * @return
	 */
	public static String trim(String value) {
		if(value == null) {
			return null;
		}
		return value.trim();
	}
	
	/**
	 * byte string to hex string
	 * @param b
	 * @return
	 */
	public static String byte2hex(byte[] b) {
		if(b == null) {
			return null;
		}
		if(b.length == 0) {
			return "";
		}
		StringBuilder retBuilder = new StringBuilder(); 
		for (int n = 0; n < b.length; ++n) {
			retBuilder.append(HEX_ARRAYS[(b[n] & 0xF0) >> 4]);
			retBuilder.append(HEX_ARRAYS[b[n] & 0x0F]);
		}
		return retBuilder.toString();
	}

	/**
	 * hex string to byte string
	 * @param str
	 * @return
	 */
	public static byte[] hex2byte(String str) {
		if (str == null)
			return null;
		str = str.trim();
		int len = str.length();
		if ((len == 0) || (len % 2 == 1))
			return null;
		byte[] b = new byte[len / 2];
		byte tmp = 0;
		char[] strs = str.toCharArray();
		for(int i = 0; i < len; i++) {
			byte t = 0;
			if(strs[i] >= '0' && strs[i] <= '9') {
				t = (byte)(strs[i] - '0');
			} else if(strs[i] >= 'A' && strs[i] <= 'F') {
				t = (byte)(strs[i] - 'A' + 10);
			} else if(strs[i] >= 'a' && strs[i] <= 'f') {
				t = (byte)(strs[i] - 'a' + 10);
			}
			if((i & 0x1) == 1) {
				tmp <<= 4;
				tmp += t;
				b[i/2] = tmp;
				tmp = 0;
			} else {
				tmp = t;
			}
		}
		return b;
	}
	
	/**
     * 判断一个字符串是否只包含数字(10进制)字符
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
   	 if(isEmpty(str)) {
   		 return false;
   	 }
   	 char[] arr = str.toCharArray();
   	 for(char c : arr) {
   		 if(!Character.isDigit(c)) {
   			 return false;
   		 }
   	 }
   	 return true;
    }
    
    /**
	 * 混淆字串串(显示前pn个字符和后tn个字符，其余全部用*填充)
	 * @param source
	 * @param pn
	 * @param tn
	 * @param mixChar
	 * @return
	 */
	public static String mix(String source, int pn, int tn) {
		return mix(source, pn, tn, '*');
	}
	/**
	 * 混淆字串串(显示前pn个字符和后tn个字符，其余前部用mixChar填充)
	 * @param source
	 * @param pn
	 * @param tn
	 * @param mixChar
	 * @return
	 */
	public static String mix(String source, int pn, int tn, char mixChar) {
		if(source == null || source.length() <= pn + tn) {
			return source;
		}
		int len = source.length();
		StringBuilder tmp = new StringBuilder(source.length());
		char[] mobileAs = source.toCharArray();
		for(int i = 0; i < pn; i++) {
			tmp.append(mobileAs[i]);
		}
		for(int i = 0; i < len - (pn + tn); i++) {
			tmp.append(mixChar);
		}
		for(int i = len - tn; i < len; i++) {
			tmp.append(mobileAs[i]);
		}
		
		return tmp.toString();
	}

}
